package com.cefa.finance.controller;

import com.cefa.finance.model.FileContractBlockData;
import com.cefa.finance.result.ResultModel;
import com.cefa.finance.service.FinanceLeaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("financeLease")
public class FinanceLeaseController {

    private static final Logger logger = LoggerFactory.getLogger(FinanceLeaseController.class);

    @Autowired
    private FinanceLeaseService financeLeaseService;

    @RequestMapping(path = "recordInfo", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel recordInfo(@RequestParam("file[]") MultipartFile[] files, @RequestParam("user") String userStr,
                                  @RequestParam("vehicle") String vehicleStr, @RequestParam("recordUser") Long loginUserId,
                                  @RequestParam("contract") String contractStr) {
        try {
            return financeLeaseService.recordInfo(files, userStr, vehicleStr, contractStr, loginUserId);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }

    @RequestMapping(path = "listLeaseInfo", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel listLeaseInfo(String searchText, Integer pageNo, Integer pageCount) {
        try {
            Map<String, Object> result = financeLeaseService.listLeaseInfo(searchText, pageNo, pageCount);
            return ResultModel.OK(result);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }

    @RequestMapping(path = "downloadContract", method = RequestMethod.GET)
    @CrossOrigin
    public ResultModel downloadContract(HttpServletRequest request, HttpServletResponse response, String fileName) {
        try {
            return financeLeaseService.downloadContract(request,response, fileName);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }


    @RequestMapping(path = "updateInfo/{contractId}", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel updateRecordInfo(@RequestParam("file[]") MultipartFile[] files, @RequestParam("user") String userStr, @RequestParam("vehicle") String vehicleStr,
                                        @RequestParam("contract") String contractStr, @RequestParam(value = "recordUser",required = true) Long recordUser,
                                        @PathVariable Long contractId, @RequestParam("updateFile") Boolean updateFile) {
        try {
            if(recordUser==null || "".equals(recordUser.toString())){
                return ResultModel.ERROR("RecordUser parameter is false!");
            }
            return financeLeaseService.updateRecordInfo(files, userStr, vehicleStr, contractStr, contractId, updateFile, recordUser);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }


    @RequestMapping(path = "getBlockDataInfo", method = RequestMethod.POST)
    @CrossOrigin
    public ResultModel getBlockDataInfo(Long contractId) {
        try {
            List<FileContractBlockData> result = financeLeaseService.getBlockDataInfo(contractId);
            return ResultModel.OK(result);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultModel.ERROR(e.getMessage());
        }
    }
}
